package com.burgeon.backend.Rest.Template.Recharge;
/**
 * 按日期获取充值单
 */
import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.RechargeData;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class FindRechargeData extends RESTBase {


    private String ip;
    private String account;
    private String password;

    public FindRechargeData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public ArrayList<RechargeData> getData() throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(), url);

      /*  String count = new JSONArray(str).getJSONObject(0).getString("count");*/
        ArrayList<RechargeData> data = new ArrayList();

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        for (int i =0;i<jsonArray.length();i++){
            RechargeData rechargeData = new RechargeData();
            rechargeData.setId(jsonArray.getJSONArray(i).getString(0));
            rechargeData.setDate(jsonArray.getJSONArray(i).getString(1));
            rechargeData.setCode(jsonArray.getJSONArray(i).getString(2));
            rechargeData.setPrice(jsonArray.getJSONArray(i).getString(3));
            rechargeData.setBalance(jsonArray.getJSONArray(i).getString(4));
            rechargeData.setOpenid(jsonArray.getJSONArray(i).getString(5));
            rechargeData.setPhone(jsonArray.getJSONArray(i).getString(6));
            rechargeData.setGive(jsonArray.getJSONArray(i).getString(7));

            data.add(rechargeData);
        }

        return data;
    }

    private MultiValueMap Data() {

        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("BILLDATE");//单据日期
        columns.add("DOCNO");//单据编号
        columns.add("TOT_AMT_DEPOSIT");//充值金额
        columns.add("'C_VIP_ID;FA_VIPACC_ID;AMOUNT'");//可用余额
        columns.add("'C_VIP_ID;OPENID'");
        columns.add("'C_VIP_ID;MOBIL'");
        columns.add("TOT_AMT_GIVE"); //赠送金额


        params.put("columns", columns);
        params.put("table", "B_VIPMONEY");//表名

        params.put("count", "true");
        HashMap orderby = new HashMap();

        orderby.put("column", "BILLDATE");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String sTime = sdf.format(date);
        HashMap expr1v1 = new HashMap();
        HashMap expr1v2 = new HashMap();

        expr1.put("combine", "and");
        expr1v1.put("column", "BILLDATE");
        expr1v1.put("condition", "'="+sTime +"'");
        expr1.put("expr1", expr1v1);
        expr1v2.put("column", "'C_VIP_ID;OPENID'");
        expr1v2.put("condition", "is not null");
        expr1.put("expr2", expr1v2);

        expr2.put("column", "STATUS");
        expr2.put("condition", "2");

        paramss.put("combine", "and");//关系

        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);

        params.put("params", paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindRechargeData g = new FindRechargeData("39.100.45.8:55322", "nea@burgeon.com.cn", "123");
        try {
            System.err.println(g.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
